package utd.cs6322.cluster;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JFileChooser;

import net.sf.javaml.clustering.FarthestFirst;
import net.sf.javaml.clustering.KMeans;
import net.sf.javaml.clustering.KMedoids;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.tools.data.FileHandler;

import utd.cs6322.cluster.clustering.ClusteringManager;
import utd.cs6322.cluster.document.DocumentManager;
import utd.cs6322.cluster.visualization.PrefuseTreeVisualizer;

/**
 * The application's main frame.
 */
public class ClusteringTool extends JApplet {

	Dataset data;
	PrefuseTreeVisualizer visKMeans = null;
	PrefuseTreeVisualizer visFFirst = null;
	PrefuseTreeVisualizer visKMedoid = null;
	
	DocumentManager docMan;
	ClusteringManager clustMan;

	public ClusteringTool() {
		visKMeans = new PrefuseTreeVisualizer();
		visFFirst = new PrefuseTreeVisualizer();
		visKMedoid = new PrefuseTreeVisualizer();
		docMan = new DocumentManager();
		clustMan = new ClusteringManager(4);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		mainPanel = new javax.swing.JPanel();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jTabbedPane1 = new javax.swing.JTabbedPane();
		jPanel1 = new javax.swing.JPanel();
		jPanel2 = new javax.swing.JPanel();
		jPanel3 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		statusPanel = new javax.swing.JPanel();
		javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
		statusMessageLabel = new javax.swing.JLabel();
		statusAnimationLabel = new javax.swing.JLabel();
		progressBar = new javax.swing.JProgressBar();

		mainPanel.setName("mainPanel"); // NOI18N

		jButton1.setText("Select File"); // NOI18N
		jButton1.setName("jButton1"); // NOI18N
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton1MouseClicked(evt);
			}
		});

		jButton2.setText("Run"); // NOI18N
		jButton2.setName("jButton2"); // NOI18N
		jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton2MouseClicked(evt);
			}
		});

		jTabbedPane1.setName("jTabbedPane1"); // NOI18N

		jPanel1.setName("jPanel1"); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 299,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 209,
				Short.MAX_VALUE));

		jTabbedPane1.addTab("KMeans", jPanel1); // NOI18N

		jPanel2.setName("jPanel2"); // NOI18N

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 299,
				Short.MAX_VALUE));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 209,
				Short.MAX_VALUE));

		jTabbedPane1.addTab("FarthestFirst", jPanel2); // NOI18N

		jPanel3.setName("jPanel3"); // NOI18N

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 299,
				Short.MAX_VALUE));
		jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 209,
				Short.MAX_VALUE));

		jTabbedPane1.addTab("KMedoids", jPanel3); // NOI18N

		visKMeans.addDisplayToPanel(jPanel1);
		visFFirst.addDisplayToPanel(jPanel2);
		visKMedoid.addDisplayToPanel(jPanel3);

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jTextArea1.setName("jTextArea1"); // NOI18N
		jScrollPane1.setViewportView(jTextArea1);

		jLabel1.setText("Results"); // NOI18N
		jLabel1.setName("jLabel1"); // NOI18N

		jLabel2.setFont(new Font("Serif", Font.BOLD, 24)); // NOI18N
		jLabel2.setForeground(Color.DARK_GRAY); // NOI18N
		jLabel2.setText("Clustering Tool"); // NOI18N
		jLabel2.setName("jLabel2"); // NOI18N

		javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
				mainPanel);
		mainPanel.setLayout(mainPanelLayout);
		mainPanelLayout
				.setHorizontalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								mainPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																mainPanelLayout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1)
																		.addGap(
																				74,
																				74,
																				74))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																mainPanelLayout
																		.createSequentialGroup()
																		.addGroup(
																				mainPanelLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addGroup(
																								mainPanelLayout
																										.createSequentialGroup()
																										.addComponent(
																												jTabbedPane1,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												304,
																												Short.MAX_VALUE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												jScrollPane1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								mainPanelLayout
																										.createSequentialGroup()
																										.addGap(
																												11,
																												11,
																												11)
																										.addComponent(
																												jLabel2)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												174,
																												Short.MAX_VALUE)
																										.addComponent(
																												jButton1)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												jButton2)))
																		.addContainerGap()))));
		mainPanelLayout
				.setVerticalGroup(mainPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								mainPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																mainPanelLayout
																		.createSequentialGroup()
																		.addGroup(
																				mainPanelLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jButton2)
																						.addComponent(
																								jButton1))
																		.addGap(
																				1,
																				1,
																				1)
																		.addComponent(
																				jLabel1)
																		.addGap(
																				3,
																				3,
																				3))
														.addGroup(
																mainPanelLayout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel2)
																		.addGap(
																				18,
																				18,
																				18)))
										.addGroup(
												mainPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jTabbedPane1,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																237,
																Short.MAX_VALUE)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																237,
																Short.MAX_VALUE))
										.addContainerGap()));

		statusPanel.setName("statusPanel"); // NOI18N

		statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

		statusMessageLabel.setName("statusMessageLabel"); // NOI18N

		statusAnimationLabel
				.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

		javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(
				statusPanel);
		statusPanel.setLayout(statusPanelLayout);
		statusPanelLayout
				.setHorizontalGroup(statusPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(statusPanelSeparator,
								javax.swing.GroupLayout.DEFAULT_SIZE, 500,
								Short.MAX_VALUE)
						.addGroup(
								statusPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(statusMessageLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												480, Short.MAX_VALUE)
										.addComponent(statusAnimationLabel)
										.addContainerGap()));
		statusPanelLayout
				.setVerticalGroup(statusPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								statusPanelLayout
										.createSequentialGroup()
										.addComponent(
												statusPanelSeparator,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												2,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												statusPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																statusMessageLabel)
														.addComponent(
																statusAnimationLabel))
										.addGap(3, 3, 3)));

		progressBar.setName("progressBar"); // NOI18N

		add(mainPanel);
	}// </editor-fold>

	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
		JFileChooser fChooser = new JFileChooser();
		fChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int aresult = fChooser.showOpenDialog(null);
		if (aresult == JFileChooser.CANCEL_OPTION) {
			System.exit(0);
		}
		File fName = fChooser.getSelectedFile();

		docMan.readDocument(fName);
	}

	private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
		if(docMan.hasData()){
			clustMan.cluster(docMan.getData());
		}
	}

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTabbedPane jTabbedPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JPanel mainPanel;
	private javax.swing.JProgressBar progressBar;
	private javax.swing.JLabel statusAnimationLabel;
	private javax.swing.JLabel statusMessageLabel;
	private javax.swing.JPanel statusPanel;
	// End of variables declaration

}